﻿Imports CRM.BusinessLayer
Imports System.Data
Partial Public Class frmDependants

    Public _AddNewPerson As Boolean

    ' xxx delete this Child and Parent code as is now done in GlobalCRM
    'Private Shared _ChildDescription As String
    'Private Shared _ParentDescription As String

    'Public Property ParentDescription() As String
    '    Get
    '        Return _ParentDescription
    '    End Get
    '    Set(ByVal value As String)
    '        _ParentDescription = value
    '    End Set
    'End Property

    'Public Property ChildDescription() As String
    '    Get
    '        Return _ChildDescription
    '    End Get
    '    Set(ByVal value As String)
    '        _ChildDescription = value
    '    End Set
    'End Property

    Private Sub frmDependants_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        With Me.drpRelationship
            .DisplayMemberPath = "Rel_ParentName" 'rel_id
            .SelectedValuePath = "Rel_ChildName"
        End With
        DataBinding.BindComboBox(CRM.BusinessLayer.CommonDataFunctions.RelationshipList, drpRelationship)
        Me.drpRelationship.SelectedIndex = -1
        Dim itemsDependents, itemsDependentsOf As DataTable
        itemsDependents = CRM.BusinessLayer.People.GetDependants(CRM.BusinessLayer.GlobalCRM.CurrentCandidateID)
        DataBinding.BindListView(itemsDependents, Me.lvwDependants)
        itemsDependentsOf = CRM.BusinessLayer.People.GetDependantOf(CRM.BusinessLayer.GlobalCRM.CurrentCandidateID)
        DataBinding.BindListView(itemsDependentsOf, Me.lvwDependantOf)

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAdd.Click


        Dim LngParentID As Long, LngChildID As Long
        Dim StrP As String, StrC As String, StrRole As String
        Dim strChildID As String
        Dim PublicRelationship As Boolean

        If Me.drpRelationship.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Please choose relationship first", CommonDialog.CustomDialogIcons.Information, "Error")
            Me.drpRelationship.Focus()
            Exit Sub
        End If

        StrP = Me.drpRelationship.SelectedValue.ToString()
        StrC = Me.drpRelationship.Text
        LngParentID = CRM.BusinessLayer.GlobalCRM.CurrentCandidateID

        'StrP = Me.drpRelationship.Text
        'StrC = Me.drpRelationship.SelectedValue

        If Me.chkCreateNew.IsChecked = True Then
            'Creating a new person, open up person form and pass details there. Processing continues there after save

            Me._AddNewPerson = True

            CRM.BusinessLayer.GlobalCRM.CurrentDependentParentDescription = StrP
            CRM.BusinessLayer.GlobalCRM.CurrentDependentChildDescription = StrC

            ' Me._ParentDescription = StrP
            ' Me._ChildDescription = StrC

            Me.Hide()

            Exit Sub
        End If

        'Not adding new so prompt for ID

        strChildID = InputBox("Please enter ID of dependant to add", "Add Dependant")
        If Not IsNumeric(strChildID) Then
            CommonFuncs.DisplayMessage("Invalid ID, action cancelled", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        LngChildID = CLng(strChildID)

        If LngChildID = CRM.BusinessLayer.GlobalCRM.CurrentCandidateID Then
            CommonFuncs.DisplayMessage("Cannot make someone a dependant of themselves!", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        Dim myPerson As CRM.BusinessLayer.People.PersonInfo = CRM.BusinessLayer.People.GetPerson(LngChildID)

        If myPerson.PersonId = 0 Then
            CommonFuncs.DisplayMessage("No person found with ID no. " & LngChildID & ". Please check and try again", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        If myPerson.CompanyId <> 0 Then
            'Child is linked to a company, cannot add. Build a suitable message for the user.
            Dim myCompany As CRM.BusinessLayer.Company.CompanyInfo

            'Get Role Type
            StrRole = CRM.BusinessLayer.Company.GetCompanyRoleName(myPerson.CompanyRoleId)

            'Get Company Name
            myCompany = CRM.BusinessLayer.Company.GetCompany(myPerson.CompanyId)

            If myCompany.CompanyID = 0 Then
                CommonFuncs.DisplayMessage(myPerson.Firstname & " " & myPerson.Surname & " is listed as being a " & StrRole & " for a company which can not be found. Action cacelled, please check with IT", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If

            CommonFuncs.DisplayMessage(myPerson.Firstname & " " & myPerson.Surname & " is listed as being a " & StrRole & " for the company '" & myCompany.CompanyName & " (" & myCompany.CompanyID & ") and therefore cannot be made a dependant.", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        If CRM.BusinessLayer.GlobalCRM.CurrentCompanyID > 0 Then
            If MsgBox("Should this be a public relationship? (i.e. visible in company tracking system)", vbYesNo + vbQuestion, "Confirm status") = MsgBoxResult.Yes Then
                PublicRelationship = True
            End If
        End If

        If MsgBox("Are you sure you want to add " & myPerson.Firstname & " " & myPerson.Surname & " to this person as a " & StrC & "?", vbYesNo, "Confirm") <> MsgBoxResult.Yes Then
            CommonFuncs.DisplayMessage("Action cancelled", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        If CRM.BusinessLayer.People.CreateDependant(LngParentID, LngChildID, StrP, StrC, CRM.BusinessLayer.GlobalCRM.CurrentConName, PublicRelationship, myPerson.Firstname & " " & myPerson.Surname) = 0 Then
            Exit Sub
        End If

        ' refresh list
        Dim itemsDependents As DataTable
        itemsDependents = CRM.BusinessLayer.People.GetDependants(CRM.BusinessLayer.GlobalCRM.CurrentCandidateID)
        DataBinding.BindListView(itemsDependents, Me.lvwDependants)

        'DataBinding.BindListView(CRM.BusinessLayer.People.GetDependants(CRM.BusinessLayer.GlobalCRM.CurrentCandidateID), Me.lvwDependants)

    End Sub

    Private Sub drpRelationship_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpRelationship.SelectionChanged
        If Me.drpRelationship.SelectedIndex >= 0 Then
            Me.btnAdd.IsEnabled = True
        Else
            Me.btnAdd.IsEnabled = False
        End If
    End Sub

    Private Sub btnSpouse_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSpouse.Click
        If Me.lvwDependants.SelectedItems.Count = 0 Then
            CommonFuncs.DisplayMessage("Please select person to be marked as partner", CommonDialog.CustomDialogIcons.Information, "Error")
            Me.lvwDependants.Focus()
            Exit Sub
        End If
        Dim currentRow As DataRowView = CType(lvwDependants.SelectedItem, DataRowView)
        If CRM.BusinessLayer.People.SetDependantSpouse(CRM.BusinessLayer.GlobalCRM.CurrentCandidateID, currentRow(0), currentRow(2)) Then
            Dim itemsDependents As DataTable
            itemsDependents = CRM.BusinessLayer.People.GetDependants(CRM.BusinessLayer.GlobalCRM.CurrentCandidateID)
            DataBinding.BindListView(itemsDependents, Me.lvwDependants)
            'DataBinding.BindListView(People.GetDependants(GlobalCRM.CurrentCandidateID), Me.lvwDependants)
        End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click

        If Me.lvwDependants.SelectedItems.Count = 0 Then
            CommonFuncs.DisplayMessage("Please select dependant to delete first.", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If
        Dim currentRow As DataRowView = CType(lvwDependants.SelectedItem, DataRowView)
        If MsgBox("Are you sure you want to remove " & currentRow(2) & " from the dependants list?", vbYesNo + vbQuestion, "Confirm delete") <> MsgBoxResult.Yes Then
            CommonFuncs.DisplayMessage("Action cancelled", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        If CRM.BusinessLayer.People.DeleteDependant(currentRow(4), currentRow(2), CRM.BusinessLayer.GlobalCRM.CurrentCandidateID, currentRow(0)) Then
            Dim itemsDependents As DataTable
            itemsDependents = CRM.BusinessLayer.People.GetDependants(CRM.BusinessLayer.GlobalCRM.CurrentCandidateID)
            DataBinding.BindListView(itemsDependents, Me.lvwDependants)
            'DataBinding.BindListView(CRM.BusinessLayer.People.GetDependants(CRM.BusinessLayer.GlobalCRM.CurrentCandidateID), Me.lvwDependants)
        End If

    End Sub

    Private Sub btnPubPriv_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPubPriv.Click
        If Me.lvwDependants.SelectedItems.Count = 0 Then
            CommonFuncs.DisplayMessage("Please select a dependant first", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If
        Dim currentRow As DataRowView = CType(lvwDependants.SelectedItem, DataRowView)
        If CRM.BusinessLayer.People.SetDependantPublicPrivate(currentRow(4), currentRow(3), currentRow(2), CRM.BusinessLayer.GlobalCRM.CurrentCandidateID) Then
            Dim itemsDependents As DataTable
            itemsDependents = CRM.BusinessLayer.People.GetDependants(CRM.BusinessLayer.GlobalCRM.CurrentCandidateID)
            DataBinding.BindListView(itemsDependents, Me.lvwDependants)
            'DataBinding.BindListView(People.GetDependants(CRM.BusinessLayer.GlobalCRM.CurrentCandidateID), Me.lvwDependants)
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub lvwDependants_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles lvwDependants.SelectionChanged
        If Me.lvwDependants.SelectedItems.Count > 0 Then
            Me.btnSpouse.IsEnabled = True
            Me.btnDelete.IsEnabled = True
            Me.btnPubPriv.IsEnabled = True
        Else
            Me.btnSpouse.IsEnabled = False
            Me.btnDelete.IsEnabled = False
            Me.btnPubPriv.IsEnabled = False
        End If
    End Sub
End Class
